package com.stottlerhenke.simbionic.editor;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.filechooser.FileFilter;


public class Util {

	static {
		
	}

	/**
	 * Hash of Files to SoftReferences of ImageIcons.
	 * 
	 * @see #getImageIcon(String, String)
	 */
  private static HashMap softImages = new HashMap();

    /**
		 * UtilActionEvent is a class that emulates jdk1.4 ActionEvents by having a
		 * setSource method. Usually setSource is used to pass events on to other
		 * listeners, with a different source. This class simplifies conversion of
		 * 1.4 code into 1.3 code, and can be used as follows: <br>
		 * UtilActionEvent event=new UtilActionEvent(actionEvent); <br>
		 * event.setSource(newSource); <br>
		 * listener.actionPerformed(event);
		 */
    public static class UtilActionEvent extends ActionEvent {

        public UtilActionEvent(ActionEvent e) {
            super(e.getSource(), e.getID(), e.getActionCommand(), e
                    .getModifiers());
        }

        public void setSource(Object newSource) {
            source = newSource;
        }
    }

    /**
		 * Utility function to return the lower-cased file extension - the part of
		 * the filename after the last period.
		 * <p>
		 * For example, getFileExtension("myPicture.is.cool.JPEG") would return
		 * "jpeg".
		 * 
		 * @param f
		 *          The file
		 * @return The file extension in lower case, or null if none exists.
		 */
    public static String getFileExtension(File f) 
    {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    static class ExtFileFilter extends FileFilter
		{
    	private String mDescription = null;
    	private String mExt = null;
    	private boolean mAcceptDir = true;

    	public ExtFileFilter(String pExt)
    	{
    		this(pExt, "");
    	}
    	
    	public ExtFileFilter(String pExt, String pDescription)
    	{
    		this(pExt, pDescription, true);
    	}
    	
    	public ExtFileFilter(String pExt, boolean pAcceptDir)
    	{
    		this(pExt, "", true);
    	}
    	
    	public ExtFileFilter(String pExt, String pDescription, boolean pAcceptDir)
    	{
    		mExt = pExt;
    		mDescription = pDescription;
    		mAcceptDir = pAcceptDir;
    	}
    	
    	public String getDescription()
			{
				return mDescription;
			}

			public boolean accept(File f)
			{
				if(f.isDirectory())
				{
					return mAcceptDir;
				}

				String ext = Util.getFileExtension(f);
				if(ext != null)
				{
					return ext.equals(mExt);
				}
				return false;
			}
		}

  	public static FileFilter createFileFilterForExt(String pExt, String pDescription)
  	{
  		return new ExtFileFilter(pExt, pDescription);
  	}

  	public static FileFilter createFileFilterForExt(String pExt, boolean pAcceptDir)
  	{
  		return new ExtFileFilter(pExt, pAcceptDir);
  	}

  	public static FileFilter createFileFilterForExt(String pExt, String pDescription, boolean pAcceptDir)
  	{
  		return new ExtFileFilter(pExt, pDescription, pAcceptDir);
  	}

    
    /**
		 * Same as getImageIcon(String, String) with null description. Uses soft
		 * references to avoid creating more than one copy of the same image, and
		 * help with memory management.
		 * 
		 * @see #getImageIcon(String, String)
		 * @param filename
		 *          Image filename (relative to IMAGE_DIR) to get
		 * @return ImageIcon for that filename, or null if no file exists
		 */
    public static ImageIcon getImageIcon(String filename) {
        return getImageIcon(filename, null);
    }

    /**
		 * Keeps a hashmap of SoftReferences to images, hashed by filename, so that
		 * the same image icon will be used for multiple versions of the same image.
		 * The SoftReferences ensure that if the images are no longer directly
		 * reachable, they will be garbage collected before an OutOfMemoryError is
		 * thrown.
		 * 
		 * @deprecated Use ImageBundle instead...
		 * 
		 * @param filename
		 *          Image filename (relative to IMAGE_DIR) to get
		 * @param description
		 *          String description of icon, or null
		 * @return
		 */
    public static ImageIcon getImageIcon(String filename, String description) {
    	
    	ResourceBundle bundle = ResourceBundle.getBundle("com.stottlerhenke.simbionic.editor.gui.ImageBundle");
    	try{
    	    ImageIcon icon= (ImageIcon) bundle.getObject(filename);
    	
    	    if (icon==null) return new ImageIcon();
    	    return icon;
    	}catch(Exception e){
    	    return new ImageIcon();
    	}
    }

}